package cn.mazexal.exambg.services;

import cn.mazexal.exambg.dataware.AdminUserMapper;
import cn.mazexal.exambg.models.db.AdminUserDO;
import cn.mazexal.exambg.utils.MD5Utils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * @author: Mazexal
 * Date: 2025/5/13
 */
@Service
public class AdminUserService {

    @Resource
    private AdminUserMapper adminUserMapper;

    public Long userLogin(String userName, String passWord) {
        QueryWrapper qry = new QueryWrapper();
        qry.eq("user_name", userName);
        AdminUserDO adminUserDO = adminUserMapper.selectOne(qry);
        if (adminUserDO == null) {
            return 0L;
        }
        String md5Password = MD5Utils.MD5(passWord);
        if (!adminUserDO.getUserPassword().equals(md5Password)) {
            return 0L;
        }
        return adminUserDO.getId();
    }

    public void addAdminUser(String userName, String passWord){
        AdminUserDO adminUserDO = new AdminUserDO();
        adminUserDO.setUserName(userName);
        adminUserDO.setUserPassword(MD5Utils.MD5(passWord));
        adminUserMapper.insert(adminUserDO);
    }
}
